<template>
  <el-dialog
    :title="title"
    :visible.sync="editDialogShow"
    :destroy-on-close="true"
    :append-to-body="true"
    width="45%"
    @close="handleClose"
    class="edit-main"
  >
    <el-form ref="formData" :model="formData" :rules="rules" label-width="80px">
      <el-form-item label="内容" prop="questionText">
        <el-input
          type="textarea"
          placeholder="请输入内容"
          v-model="formData.questionText"
          maxlength="200"
          show-word-limit
        >
        </el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="onSubmit">确 定</el-button>
      <el-button @click="handleClose">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateMsg } from "@/api/customerService/session";

export default {
  name: "messageListDialog",
  props: {
    editShow: {
      type: Boolean,
      default: false,
    },
    questionText: {
      type: String,
      default: "",
    },
    id: {
      type: String,
      default: "",
    },
    title: {
      type: String,
      default: "编辑消息",
    },
  },
  data() {
    return {
      loading: false,
      tableData: [], //表格数据
      pagination: {
        pageNum: 1,
        pageSize: 10,
      },
      formData: {
        questionText: "",
      },
      rules: {
        questionText: [
          { required: true, message: "内容不能为空", trigger: "blur" },
        ],
      },
      total: 0,
      editDialogShow: false,
    };
  },

  watch: {
    editShow: {
      handler(val) {
        if (val) {
          this.editDialogShow = val;
        }
      },
      immediate: true,
    },
    questionText: {
      handler(val) {
        if (val) {
          this.formData.questionText = val;
        }
      },
      immediate: true,
    },
  },
  methods: {
    // 保存
    onSubmit() {
      this.$refs["formData"].validate(async (valid) => {
        if (valid) {
          const params = {
            id: this.id,
            questionText: this.formData.questionText,
          };
          const { code } = await updateMsg(params);
          if (code === 200) {
            this.handleClose();
          }
        }
      });
    },

    handleClose() {
      this.editDialogShow = false;
      this.formData.questionText = "";
      this.pagination = {
        pageNum: 1,
        pageSize: 10,
      };
      this.total = 0;
      this.tableData = [];
      this.$emit("onCloseEditDialog");
    },
  },
};
</script>

<style lang="scss" scoped>
.edit-main {
  ::v-deep .el-textarea__inner {
    height: 150px;
  }
}
</style>
